package com.zhongwang.business.module.main.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.base.BaseModel;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.module.main.model.DsnProStruct;
import com.zhongwang.business.module.main.model.Main;
import com.zhongwang.business.module.main.model.ProStructTastModel;
import com.zhongwang.business.module.main.model.productMixModel;
import com.zhongwang.business.module.main.service.MainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class MainController extends BaseController {
    @Autowired
    private MainService mainService;
    /**
     * 首页跳转到款票审核
     *
     * @param model
     * @return
     */
    @GetMapping("/main")
    public String linkInvoiceList2(Model model) {

        SimpleDateFormat sdf11 = new SimpleDateFormat("yyyy");
        Date date = new Date();
        Integer year=Integer.parseInt(sdf11.format(date));

        SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
        Date date2 = new Date();
        String month=sdf2.format(date2);

        model.addAttribute("year",year);
        model.addAttribute("month",month);

//        Main model_1=mainService.selectProject();
//        Main model_2=mainService.selectProjectByYear(1,year);
//        Main model_3=mainService.selectProjectByMonth(2,month);
//
//        model.addAttribute("project1",model_1);
//        model.addAttribute("project2",model_2);
//        model.addAttribute("project3",model_3);
//
//
//
//        /**
//         * 待审核+待创建的任务
//         */
       model.addAttribute("totalCnt",mainService.selectUnCreAndAppByKind());
//
//        /**
//         * 待审核的数量
//         */
        model.addAttribute("unAppInfo",mainService.unAppInfo());

        /**
         * 首页柱状图的数据
         * @param model
         * @return
         */
        Main model1=new Main();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);

        model1.setBeginDT(weekBegin);
        model1.setEndDT(weekEnd);
        /**
         * 开始结束时间
         */
        model.addAttribute("beginDT",weekBegin);
        model.addAttribute("endDT",weekEnd);

        //model.addAttribute("barInfo",mainService.selectBarData(model1));

        /**
         * 返回空数据88888888888888888888888-----------------------------------
         */
        Main model12=new Main();

        model.addAttribute("project1",model12);
        model.addAttribute("project2",model12);
        model.addAttribute("project3",model12);
//        model.addAttribute("unAppInfo",model12);
//        model.addAttribute("totalCnt",model12);

        /**
         * 返回空数据88888888888888888888888-------------------------------------
         */

        return "/main/main";
    }
    /**
     * 已经下达的，需要合同;并且已经报价完毕的。
     */
    @ResponseBody
    @PostMapping("/main/selGroupTaskList")
    public JsonResult selGroupTaskList() {
        return jsonResult(mainService.selectGroupCnt());
    }
    /**
     * 周数据
     */
    @ResponseBody
    @PostMapping("/main/selWeekDataList")
    public JsonResult selWeekDataList(String beginDT,String endDT) {
        /**
         * 开始结束时间
         */
        Main model1=new Main();
        model1.setBeginDT(beginDT);
        model1.setEndDT(endDT);
        return jsonResult(mainService.selectBarData(model1));
    }
    /**
     * 项目总数
     */
    @ResponseBody
    @PostMapping("/main/selAllData")
    public JsonResult selAllData() {
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(mainService.selectProject());
        return jsonResult;
    }
    /**
     * 年数据
     */
    @ResponseBody
    @PostMapping("/main/selDataByYear")
    public JsonResult selDataByYear(Integer year) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(mainService.selectProjectByYear(1,year));
        return jsonResult;
    }
    /**
     * 月数据
     */
    @ResponseBody
    @PostMapping("/main/selDataByMonth")
    public JsonResult selDataByMonth(String month) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(mainService.selectProjectByMonth(2,month));
        return jsonResult;
    }



    @GetMapping("/main/productMix")
    public String page(){
        return "main/productMix";
    }
    /**
     *产品结构列表查询
     * @param productMixModel
     * @return
     * @Author: China.kxx
     * @Date: 2021/4/13 12:44
     */
    @ResponseBody
    @PostMapping("/main/productMixPage/list")
    public JsonResult list(productMixModel productMixModel) {
        startPage();
        List<productMixModel> list = mainService.productSelect(productMixModel);
        return jsonResult(list);
    }


//    /**
//     * 产品结构明细
//     */
//    @RequestMapping("/designTask/proStruct")
//    @GetMapping("/checkProStruct/{id}")
//    public String viewProPage(DsnProStruct dsnProStruct, Model model) {
//        ProStructTastModel proStructTastModel = new ProStructTastModel();
//        proStructTastModel.setId(dsnProStruct.getProId());
//        model.addAttribute("proJect", mainService.selectProductList(dsnProStruct.getProId()).get(0));
//        return "main/checkProPage";
//    }


    /**
     * 产品结构明细
     */
    @RequestMapping("/designTask/proStruct/checkProStruct/{proId}")
    public String checkProPage(@PathVariable("proId") Integer proId, Model model) {
        model.addAttribute("proJect", mainService.selectProjectInfo(proId));
        return "main/checkProPage";
    }

    /**
     * 审核页面产品结构列表
     */
    @ResponseBody
    @PostMapping("/designTask/proStruct/proStructCheckList")
    public JsonResult findCheckProStructByQuery(Integer proId) {
        startPage();
        List<DsnProStruct> list = mainService.findCheckProStructByQuery(proId);
        return jsonResult(list);
    }
}
